<?php

namespace app\user\controller;

use app\model\UserMessage as ModelUserMessage;
use app\model\UserPassword as ModelUserPassword;

/**
 * 订单相关类
 * Class Common
 * @package app\user\controller
 */
class Message extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 信息列表 \n
     * URI : /message.list
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填  | 登录凭证
     *  page          |  int   | 必填  | 页码
     *  listRows      |  int   | 必填  | 条数
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  totalPages  | string | 总页数
     *  list        | array  | 列表
     *
     * list
     *  name         |  type  | description
     * --------------|--------|-------------
     *  messageId    | int    | id
     *  title        | string | 标题
     *  type         | string | 类型
     *  aboutId      | string | 相关id
     *  gmtCreate    | string | 时间
     */
    public function messageList()
    {
        $where['user_id'] = $this->user_id;

        $int_now_page = $this->_getContentParams('page');//当前页码
        _toValidate('id', $int_now_page, 10005);

        $int_list_rows = $this->_getContentParams('listRows');//一页的条数
        _toValidate('id', $int_list_rows, 10006);

        $int_total_rows = ModelUserMessage::getInstance()->toCount($where);//总条数
        $int_total_pages = ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '==', true, 10007);

        $int_first_row = ($int_now_page - 1) * $int_list_rows;

        //总列表
        $arr_list = (array)ModelUserMessage::getInstance()->toSelect(
            $where,
            'id as messageId,title,type,about_id as aboutId,gmt_create AS gmtCreate',
            $int_first_row,
            $int_list_rows,
            'gmt_create desc'
        );

        _returnData(['totalPages' => $int_total_pages, 'list' => $arr_list]);
    }

    /**
     * 消息详情 \n
     * URI : /message.info
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填  | 登录凭证
     *  messageId     | int    | 必填  | 消息id
     *
     * @return
     *  name              |  type  | description
     * -------------------|--------|-------------
     *  info              | array  | 详情
     *
     * info
     *  name              |  type  | description
     * -------------------|--------|-------------
     *  content           | string | 详情
     */
    public function messageInfo()
    {
        $message_id = $this->_getContentParams('messageId');
        $bool_res = ModelUserMessage::getInstance()->checkMessageId($message_id);
        _toCompare($bool_res, '===', false, 10009);

        $info = ModelUserMessage::getInstance()->toFind(['id' => $message_id], 'content');

        _returnData(['info' => $info]);
    }
}
